package com.pactera.asmp.server.utils;

import com.alibaba.fastjson.JSONObject;
import com.pactera.asmp.server.entity.PermissionVO;
import com.pactera.asmp.server.entity.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: tlsserver
 * @Package com.pactera.asmp.server.utils
 * @ClassName: TreeNodeUtils
 * @Description: 元数据树形节点转换
 * @Date 2020/06/09 16:23
 * @Author lvzhiqiang
 * @Version 1.0
 */
public class TreeNodeUtils {

    /**
     * dataToTreeNodeJson
     * @param data
     * @return
     */
    public static  List<TreeNode> dataToTreeNodeJson(List<PermissionVO> data){
        List<PermissionVO> childrenNodeData = new ArrayList<>();
        List<TreeNode> treeNodes = new ArrayList<>();
        String tagParent = "0";
        for(int i = 0 ;i<data.size(); i++){
            PermissionVO item = data.get(i);
            if(item.getIstype().equals(tagParent) && item.getpId().equals(tagParent)){
                childrenNodeData = getChildrenNodeData(item.getId(), data);
                if(childrenNodeData.size()>0){
                    treeNodes.add(new TreeNode(item, childrenNodeData));
                }else {
                    treeNodes.add(new TreeNode(item, null));
                }
            }
        }

        return treeNodes;
    }

    /**
     * getChildrenNodeData
     * @param parentId
     * @param data
     * @return
     */
    private static List<PermissionVO> getChildrenNodeData(String parentId, List<PermissionVO> data){
        List<PermissionVO> childrenNodeData = new ArrayList<>();
        for(int i = 0 ;i<data.size(); i++){
            PermissionVO item = data.get(i);
            if(item.getpId().equals(parentId)){
                childrenNodeData.add(data.get(i));
            }
        }
        return childrenNodeData;
    }

}
